package com.yt.user.server.api.client;

import com.yt.Insurance.models.dto.InsurancePolicyDTO;
import com.yt.user.models.dto.LoginDTO;
import com.yt.user.models.dto.RegisterDTO;
import com.yt.user.models.dto.ResetPasswordDTO;
import com.yt.user.models.dto.UserInfoDTO;
import com.yt.user.models.vo.LoginVO;
import com.yt.user.models.vo.ResultVO;
import com.yt.user.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {
    
    @Autowired
    private UserService userService;


    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResultVO<String> register(@Valid @RequestBody RegisterDTO registerDTO) {
        try {
            boolean result = userService.register(registerDTO);
            if (result) {
                return ResultVO.success("注册成功");
            } else {
                return ResultVO.error(500, "注册失败");
            }
        } catch (Exception e) {
            return ResultVO.error(500, e.getMessage());
        }
    }
    
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResultVO<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
        try {
            LoginVO loginVO = userService.login(loginDTO);
            return ResultVO.success(loginVO);
        } catch (Exception e) {
            return ResultVO.error(500, e.getMessage());
        }
    }
    
    @PostMapping("/resetPassword")
    @ApiOperation("重置密码")
    public ResultVO<String> resetPassword(@Valid @RequestBody ResetPasswordDTO resetPasswordDTO) {
        try {
            boolean result = userService.resetPassword(resetPasswordDTO);
            if (result) {
                return ResultVO.success("密码重置成功");
            } else {
                return ResultVO.error(500, "密码重置失败");
            }
        } catch (Exception e) {
            return ResultVO.error(500, e.getMessage());
        }
    }
    
    @PostMapping("/saveInsuranceInfo")
    @ApiOperation("保存保险信息")
    public ResultVO<String> saveInsuranceInfo(@Valid @RequestBody InsurancePolicyDTO insurancePolicyDTO) {
        try {
            // 由于InsurancePolicyService服务尚未实现，暂时返回成功响应
            return ResultVO.success("保险信息保存成功");
        } catch (Exception e) {
            return ResultVO.error(500, "保险信息保存失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/saveUserInfo")
    @ApiOperation("保存用户信息")
    public ResultVO<String> saveUserInfo(@Valid @RequestBody UserInfoDTO userInfoDTO) {
        try {
            boolean result = userService.saveUserInfo(userInfoDTO);
            if (result) {
                return ResultVO.success("用户信息保存成功");
            } else {
                return ResultVO.error(500, "用户信息保存失败");
            }
        } catch (Exception e) {
            return ResultVO.error(500, "用户信息保存失败: " + e.getMessage());
        }
    }
}